<template>
  <el-container class="main-container">
    <!-- 头部区域 -->
    <el-header>
      <!-- 左侧的 logo -->
      <img src="../../assets/images/logo.png" alt="" />
      <!-- 右侧的菜单 -->
      <el-menu
        router
        class="el-menu-top"
        mode="horizontal"
        background-color="#23262E"
        text-color="#fff"
        active-text-color="#409EFF"
      >
        <el-submenu index="1">
          <template slot="title">
            <!-- 头像 -->
            <img v-if="userInfo.user_pic" :src="userInfo.user_pic" alt="" class="avatar" />
            <img v-else src="../../assets/logo.png" alt="" class="avatar" />
            <span>个人中心</span>
          </template>
          <template v-if="menus[2]">
            <el-menu-item v-for="item in menus[2].children" :key="item.indexPath" :index="item.indexPath" ><i class="el-icon-s-operation"></i>{{item.title}}</el-menu-item>
          </template>

        </el-submenu>
          <!-- <el-menu-item index="1-1"><i class="el-icon-s-operation"></i>基本资料</el-menu-item>
          <el-menu-item index="1-2"><i class="el-icon-camera"></i>更换头像</el-menu-item>
          <el-menu-item index="1-3"><i class="el-icon-key"></i>重置密码</el-menu-item>
        </el-submenu> -->
        <el-menu-item index="#" @click="logout"><i class="el-icon-switch-button"></i>退出</el-menu-item>
      </el-menu>
    </el-header>
    <el-container>
      <!-- 侧边栏区域 -->
      <el-aside width="200px">
        <div class="user-box">
          <!-- v-bind绑定图片地址，则不会打包 -->
          <img v-if="userInfo.user_pic" src="userInfo.user_pic" alt="" class="avatar"/>
          <img v-else src="../../assets/logo.png" alt="" />
          <span>欢迎 {{ userInfo.nickname ||userInfo.username }}</span>
        </div>
        <!-- default-active 默认高亮，设置的是菜单项的index-->
         <!-- :router:"true"必须加 ： 才是属性绑定，才有类型 -->
        <!-- $route路由路径 -->
          <el-menu
            :default-active="$route.path"
            background-color="#23262E"
            text-color="#fff"
            active-text-color="#409EFF"
            router
          >
          <!--菜单分为两种类型
          1.有子菜单的：el-submenu
          2.没有子菜单的：el-menu-item

          template是虚拟的标签
          不会渲染到DOM树上，只是起到了包裹的作用
          -->
          <!-- index是唯一的标识符  -->
          <template v-for="item in menus">
            <!-- 不包含子菜单的“一级菜单” -->
            <el-menu-item :index="item.indexPath" :key="item.indexPath" v-if="!item.children">
              <i :class="item.icon"></i>{{ item.title }}
            </el-menu-item>
            <!-- 包含子菜单的“一级菜单” -->
            <el-submenu :index="item.indexPath" :key=" item.indexPath" v-else>
              <template slot="title">
                <i :class="item.icon"></i>
                <span>{{ item.title }}</span>
              </template>
              <!-- 循环渲染“二级菜单” -->
              <el-menu-item :index="subItem.indexPath" v-for="subItem in item.children" :key="subItem.indexPath">
                <i :class="subItem.icon"></i>{{ subItem.title }}
              </el-menu-item>
            </el-submenu>
          </template>
        </el-menu>
      </el-aside>
      <el-container>
        <!-- 页面主体区域 -->
        <el-main>
          <!-- 二级路由的占位 -->
           <router-view></router-view>
        </el-main>
        <!-- 底部 footer 区域 -->
        <el-footer>© www.itheima.com - 黑马程序员</el-footer>
      </el-container>
    </el-container>
  </el-container>
</template>

<script>
// 导入辅助函数
import { mapState } from 'vuex'
export default {
  name: 'upMain',
  data () {
    return {
      menus: []
    }
  },
  methods: {
    // 退出登录
    logout () {
      // $confirm用于提醒用户
      // 第一个参数是提示的内容
      // 第二个参数是标题
      // 第三个参数是一个对象，用于配置按钮的文本和类型
      this.$confirm('您确认退出登录吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          // 1. 清空 token
          this.$store.commit('updateToken', '')
          // 2. 跳转到登录页面
          this.$router.push('/login')
        })
        .catch(err => err)
    },
    // 发请求获取menus
    async getMenus () {
      try {
      // 1. 发请求获取menus
        const { data: res } = await this.$http.get('/my/menus')
        // console.log(res)
        // 2. 将menus存储到vuex中
        this.menus = res.data
      } catch (err) {
        // 捕获错误，不处理
      }
    }
  },
  created () {
    this.getMenus()
    this.$store.dispatch('user/getUserInfo')
  },
  computed: {
    // 计算属性，用于获取 token
    ...mapState('user', ['userInfo', 'token'])
  }
}
</script>

<style lang="less" scoped>
.main-container {
  height: 100%;
  .el-header,
  .el-aside {
    background-color: #23262e;
  }
  .el-header {
    padding: 0;
    display: flex;
    justify-content: space-between;
  }
  .el-main {
    overflow-y: scroll;
    height: 0;
    background-color: #F2F2F2;
  }
  .el-footer {
    background-color: #eee;
    font-size: 12px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
.user-box {
  height: 70px;
  display: flex;
  justify-content: center;
  align-items: center;
  border-top: 1px solid #000;
  border-bottom: 1px solid #000;
  user-select: none;
  img {
    width: 35px;
    height: 35px;
    border-radius: 50%;
    background-color: #fff;
    margin-right: 15px;
    object-fit: cover;
  }
  span {
    color: white;
    font-size: 12px;
  }
}
.avatar {
  border-radius: 50%;
  width: 35px;
  height: 35px;
  background-color: #fff;
  margin-right: 10px;
  object-fit: cover;
}
</style>
